package cn.newgrand.netcall.integration.message;

import cn.newgrand.common.StringUtil;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-11-3 下午04:23:06
 * 类说明
 * 
 */
public class MessageParser {
	private String bodyXML;
	private static final String CONTENT_START_TAG="<content>"; 
	private static final String CONTENT_END_TAG="</content>"; 
	private static final String BUSINESSID_START_TAG="<businessid>"; 
	private static final String BUSINESSID_END_TAG="</businessid>"; 
	private static final String REMARK_START_TAG="<remark>"; 
	private static final String REMARK_END_TAG="</remark>"; 
	private static final String ADDRESS_START_TAG="<address>"; 
	private static final String ADDRESS_END_TAG="</address>"; 
	private static final String CHILD_COUNT_START_TAG="<childcount>"; 
	private static final String CHILD_COUNT_END_TAG="</childcount>"; 
	private static final String TO_GROUP_NO_START_TAG="<togroupno>"; 
	private static final String TO_GROUP_NO_END_TAG="</togroupno>"; 
	
	
	public MessageParser(String bodyXML){
		this.bodyXML =bodyXML;
	}
	
	public String getContent(){
		int start = bodyXML.indexOf(CONTENT_START_TAG);
		int end = bodyXML.indexOf(CONTENT_END_TAG);
		if(start!=-1&&end!=-1)
			return bodyXML.substring(start+CONTENT_START_TAG.length(),end);
		else
			return "";
	}
	
	public String getRemark(){
		int start = bodyXML.indexOf(REMARK_START_TAG);
		int end = bodyXML.indexOf(REMARK_END_TAG);
		if(start!=-1&&end!=-1)
			return bodyXML.substring(start+REMARK_START_TAG.length(),end);
		else
			return "";
	}
	public String getBusinessID(){
		int start = bodyXML.indexOf(BUSINESSID_START_TAG);
		int end = bodyXML.indexOf(BUSINESSID_END_TAG);
		if(start!=-1&&end!=-1)
			return bodyXML.substring(start+BUSINESSID_START_TAG.length(),end);
		else
			return "";
	}
	public String getAddress(){
		int start = bodyXML.indexOf(ADDRESS_START_TAG);
		int end = bodyXML.indexOf(ADDRESS_END_TAG);
		if(start!=-1&&end!=-1)
			return bodyXML.substring(start+ADDRESS_START_TAG.length(),end);
		else
			return "";
	}
	public int getChildCount(){
		int count = 0;
		String tempStr = null;
		int start = bodyXML.indexOf(CHILD_COUNT_START_TAG);
		int end = bodyXML.indexOf(CHILD_COUNT_END_TAG);
		if(start!=-1&&end!=-1)
			tempStr = bodyXML.substring(start+CHILD_COUNT_START_TAG.length(),end);
		
		if(!StringUtil.isEmpty(tempStr)){
			count = Integer.parseInt(tempStr);
		}
		return count;
	}
	public String getToGroupNo(){
		int start = bodyXML.indexOf(TO_GROUP_NO_START_TAG);
		int end = bodyXML.indexOf(TO_GROUP_NO_END_TAG);
		if(start!=-1&&end!=-1)
			return bodyXML.substring(start+TO_GROUP_NO_START_TAG.length(),end);
		else
			return "";
	}
	
	/**
	 * content+(count)
	 * 
	 * 如内容显示：待办任务(6);
	 * @return
	 */
	public String getContentWithCount(){
		return getContent()+"("+getChildCount()+")";
	}
	 
	
	public static void main(String[] args){
		String bodyXML = " <content>应用系统有条信息需要你去处理，点击进入2</content>";
		MessageParser mp = new MessageParser(bodyXML);
		System.out.println(mp.getContent());
	}
}
